﻿using System;
using System.Xml.Serialization; 
namespace WebApplication2
{
    public class OrderService
    {
        public List<Order> orderData { get; set; }
        public OrderService() {
            Order p1 = new Order(1, "milk", "Tom");
            p1.AddDetails(1, "milk", 6, 2.00);
            p1.theWholePrice();
            Order p2 = new Order(2, "Cheese", "Jerry");
            p2.AddDetails(1, "cheese", 1, 10.00);
            p2.theWholePrice();
            Order p3 = new(3, "meat", "Spike");
            p3.AddDetails(1, "meat", 3, 21.5);
            p3.theWholePrice();
            orderData = new List<Order>();
            orderData.Add(p1);
            orderData.Add(p2);
            orderData.Add(p3);
        }
        public void AddOrder(int sele, int Ono, string GName, string CusName, int Oid, string GName1, int GNum, double price)
        {
            try
            {
                int onos = Ono;
                string gnames = GName;
                string customers = CusName;
                bool equal = false;
                string gname = GName1;
                int oid = Oid;
                int num = GNum;
                double Addprice = price;
                Order o = new Order(onos, gnames, customers);
                foreach (Order i in this.orderData)
                {
                    if (i.Equals(o))
                    {
                        Console.WriteLine("请不要创建订单号重复的订单");
                        equal = true;
                    }
                }
                if (!equal)
                {
                    o.AddDetails(oid, gname, num, Addprice);
                    orderData.Add(o);
                    o.theWholePrice();
                    Console.WriteLine("订单添加成功");
                }
            }
            catch
            {
                Console.WriteLine("Error!请检查输入类型是否正确等");
            }
        }
        public void AddOrder(int sele, int Ono, int Oid, string GName1, int GNum, double price)
        {
            try
            {
                int onos1 = Ono;
                string GNamed1 = GName1;
                int oid1 = Oid;
                int num1 = GNum;
                double Addprice1 = price;
                this.orderData.ForEach(i =>
                {
                    if (i.Ono == onos1)
                    {
                        i.AddDetails(oid1, GNamed1, num1, Addprice1);
                        i.theWholePrice();
                        Console.WriteLine("添加成功！");
                    }
                });


            }
            catch
            {
                Console.WriteLine("Error!请检查输入类型是否正确等");
            }
        }
        public void DeleteOrder(int DelSel, int OnoToDel, int OidToDel = 0)
        {
            try
            {
                int dono = OnoToDel;
                int doid = OidToDel;
                switch (DelSel)
                {
                    case 1:
                        for (int i = this.orderData.Count - 1; i >= 0; i--)
                        {
                            if (this.orderData[i].Ono == dono)
                            {
                                this.orderData.Remove(this.orderData[i]);
                            }
                        }
                        Console.WriteLine("已删除");
                        break;
                    case 2:
                        for (int i = this.orderData.Count - 1; i >= 0; i--)
                        {
                            if (this.orderData[i].Ono == dono)
                            {
                                this.orderData[i].DeleteDatails(doid);
                            }
                        }
                        break;
                }

            }
            catch
            {
                Console.WriteLine("Error!请检查是否输入错误。");

            }

        }
        public void ChangeOrder(int sele3, int Eono, string Estring)
        {
            try
            {

                int m = Eono;
                int n = sele3;
                switch (n)
                {
                    case 1:
                        string CGname = Estring;
                        this.orderData.ForEach(i =>
                        {
                            if (i.Ono == m)
                            {
                                i.Gname = CGname;
                                Console.WriteLine("修改成功！");
                            }
                        }
                        );
                        break;
                    case 2:
                        string CCname = Estring;
                        this.orderData.ForEach(i =>
                        {
                            if (i.Ono == m)
                            {
                                i.Customer = CCname;
                                Console.WriteLine("修改成功！");
                            }
                        }
                        );
                        break;


                }
            }
            catch
            {
                Console.WriteLine("Error!请检查订单号是否错误/是否输入不存在选项等");
            }
        }
        public void ChangeOrder(int sele3, int Eono, int sele3_1, int Eoid, string gnamed = "", int gnum = 0, double price = 0.00)
        {
            try
            {
                int m, n;
                m = Eono;
                n = sele3;
                switch (n)
                {
                    case 3:
                        this.orderData.ForEach(i =>
                        {
                            if (i.Ono == m)
                            {
                                i.ChangeDetails(sele3_1, Eoid, gnamed, gnum, price);
                                i.theWholePrice();
                                Console.WriteLine("修改成功！");
                            }
                        }
                        );

                        break;


                }
            }
            catch
            {
                Console.WriteLine("Error!请检查订单号是否错误/是否输入不存在选项等");
            }
        }
        public List<Order> searchOrderByLinq(int n, int Eint1, int Eint2 = 0)
        {
            List<Order> sel = new List<Order>();
            try
            {

                switch (n)
                {
                    case 1:
                        int orderno = Eint1;
                        var query1 = from Sel1 in orderData
                                     where Sel1.Ono == orderno
                                     orderby Sel1.Oamount
                                     select Sel1;
                        sel = query1.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for (int i = 0; i < sel.Count; i++)
                        {
                            Console.WriteLine(sel[i].ToString());
                            for (int m = 0; m < sel[i].orders.Count; m++)
                            {
                                Console.WriteLine(sel[i].orders[m].ToString());
                            }
                        }
                        break;

                    case 4:
                        int MaxOamount = Eint1;
                        int MinOamount = Eint2;
                        var query4_1 = from Sel4 in orderData
                                       where Sel4.Oamount <= MaxOamount
                                       orderby Sel4.Oamount
                                       select Sel4;
                        var query4_2 = from Sel5 in query4_1
                                       where MinOamount <= Sel5.Oamount
                                       orderby Sel5.Oamount
                                       select Sel5;
                        sel = query4_2.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for (int i = 0; i < sel.Count; i++)
                        {
                            Console.WriteLine(sel[i].ToString());
                            for (int m = 0; m < sel[i].orders.Count; m++)
                            {
                                Console.WriteLine(sel[i].orders[m].ToString());
                            }
                        }
                        break;



                }
            }
            catch
            {
                Console.WriteLine("Error!请检查是否输入错误数据");
            }
            return sel;

        }
        public List<Order> searchOrderByLinq(int n, string Estring)
        {
            List<Order> sel = new List<Order>();
            try
            {

                switch (n)
                {

                    case 2:
                        string gName = Estring;
                        var query2 = from Sel2 in orderData
                                     where Sel2.Gname == gName
                                     orderby Sel2.Oamount
                                     select Sel2;
                        sel = query2.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for (int i = 0; i < sel.Count; i++)
                        {
                            Console.WriteLine(sel[i].ToString());
                            for (int m = 0; m < sel[i].orders.Count; m++)
                            {
                                Console.WriteLine(sel[i].orders[m].ToString());
                            }
                        }
                        break;
                    case 3:
                        string custoname = Estring;
                        var query3 = from Sel3 in orderData
                                     where Sel3.Customer == custoname
                                     orderby Sel3.Oamount
                                     select Sel3;
                        sel = query3.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for (int i = 0; i < sel.Count; i++)
                        {
                            Console.WriteLine(sel[i].ToString());
                            for (int m = 0; m < sel[i].orders.Count; m++)
                            {
                                Console.WriteLine(sel[i].orders[m].ToString());
                            }
                        }
                        break;
                }
            }

            catch
            {
                Console.WriteLine("Error!请检查是否输入错误数据");
            }
            return sel;
        }
        public void export()
        {
            XmlSerializer toXml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("OrderXml.xml", FileMode.Create))
            {
                toXml.Serialize(fs, this.orderData);
            }
            Console.WriteLine("已序列化为xml格式");
            Console.WriteLine(File.ReadAllText("OrderXml.xml"));
        }
        public List<Order> import()
        {
            List<Order> OD = new List<Order>();
            XmlSerializer toDeXml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("OrderXml.xml", FileMode.Open))
            {
                OD = (List<Order>)toDeXml.Deserialize(fs);
                Console.WriteLine("从OrderXml.xml反序列化结果：");
                for (int i = 0; i < OD.Count; i++)
                {
                    Console.WriteLine(OD[i].ToString());
                    for (int m = 0; m < OD[i].orders.Count; m++)
                    {
                        Console.WriteLine(OD[i].orders[m].ToString());
                    }
                }
            }
            return OD;
        }
    }
}
